<?php
class membermod extends MY_Model{
	
	function membermod(){
		parent::__construct(
			"member",
			array(
				"id_member",
				"username",
				"password",
				"images_large", //---> file type long blob (optional)
				"images_medium", //---> file type medium blob (optional)
				"images_small", //---> file type blob (optional)
				"images_tiny", //---> file type tinyblob (optional)
				"images_thumbs", //---> file type tinyblob (optional)
				"images_type", //---> jpg,png,gif
				"level", //---> level member(own,teacher,student,guest) md5
				"status", //---> active or deactive				
				"posted",
				"modified",
			),
			array("id_member")
		);
	}
	
	function generateID($data){
		return do_hash($data.(unix_to_human(time(),true,'eu')."memberNewMod").$this->table_name);
	}
	
	function add($newData){
		$newData['status'] = "active";
		//$newData['level'] = md5("user");
		$newData['posted'] = unix_to_human(time(),true,'eu');
		$this->db->insert($this->table_name, $newData);
	}
	
	function update($id,$newData){
		$newData['modified'] = unix_to_human(time(),true,'eu');
		$this->db->where($this->table_fields[0], $id);
		return $this->db->update($this->table_name, $newData);
	}
	
	function delete($ids){
		if (!is_array($ids)) $ids = array($ids);
    	foreach($ids as $pk) {
    		$this->db->or_where($this->table_fields[0], $pk);	
    	}
    	return $this->db->delete($this->table_name, $pk);
	}
	
	function remove($ids){
    	return $this->db->delete($this->table_name, array($this->table_fields[0]=>$ids));
    }
	
 	function get($id){
		return $this->db->get_where($this->table_name, array($this->table_fields[0]=>$id));
    }
    
	function get_record($limit=0,$offset=20){
		$this->db->limit($offset,$limit);
		$this->db->order_by('posted','desc');
		return $this->db->get($this->table_name);
    }
    
	function get_record_unlimit(){
		$this->db->order_by('posted','desc');
		return $this->db->get($this->table_name);
    }

	function get_where($where,$limit=0,$offset=20){
//    	return $this->db->get_where($this->table_name, $where, $limit, $offset);
		$this->db->like('id_member',$where,'match');
		$this->db->or_like('level',md5($where),'match');
		$this->db->or_like('username',$where,'both');
		$this->db->or_like('status',$where,'both');
		$this->db->or_like('posted',$where,'both');
		$this->db->order_by('posted','desc');
		$this->db->limit($offset,$limit);
    	return $this->db->get($this->table_name);
    } 
    
	function validate($user,$pass){
//		$where = "username=$user and pass=$pass";
		$where = array(
			'username' => $user,
			'password' => $pass
		);
		
		$query = $this->db->get_where($this->table_name, $where);
		
		if ($query->num_rows() > 0){
			return true;
		} else {
			return false;
		}
	}
	
	function validatemember($user){
//		$where = "username=$user and pass=$pass";
		$where = array(
			'id_member' => $user
		);
		
		$query = $this->db->get_where($this->table_name, $where);
		
		if ($query->num_rows() > 0){
			return true;
		} else {
			return false;
		}
	}
}